<template>
  <div class="wrapper">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="search">
          <span>商品名称:</span
          ><el-input v-model="shopname" placeholder="请输入商品名"></el-input>
        </div>
        <div class="block">
          <span>商品品牌：</span>
          <el-cascader
            v-model="bandvalue"
            :options="checkoptions[0].children"
            @change="handleChange"
          ></el-cascader>
        </div>
        <div class="block">
          <span>商品类型：</span>
          <el-cascader
            v-model="typevalue"
            :options="checkoptions[1].children"
            @change="handleChange2"
          ></el-cascader>
        </div>
        <div class="buttongroup">
          <el-button type="primary" @click="check">查询</el-button>
          <el-button type="primary" @click="add">新增</el-button>
        </div>
      </div>
      <div>
        <el-button @click="backtabel">返回</el-button>
      </div>
      <div class="tableform">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="id" label="id" width="180"> </el-table-column>
          <el-table-column prop="name" label="名称" width="180">
          </el-table-column>
          <el-table-column prop="band" label="品牌" width="180">
          </el-table-column>
          <el-table-column prop="type" label="类型" width="180">
          </el-table-column>
          <el-table-column prop="price" label="单价" width="180">
          </el-table-column>
          <el-table-column prop="stock" label="库存"> </el-table-column>
          <el-table-column prop="remarks" label="备注"> </el-table-column>
          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click="edit(scope.row)" type="text" size="small"
                >编辑</el-button
              >
              <el-button type="text" size="small" @click="deleteUser(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page.sync="currentPage3"
            :page-size="page_size"
            layout="prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="商品名称" prop="name">
            <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="price">
            <el-input v-model="ruleForm.price" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="品牌" prop="band">
            <el-input v-model="ruleForm.band" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-input v-model="ruleForm.type" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="库存数量" prop="stock">
            <el-input v-model="ruleForm.stock" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="ruleForm.remarks" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <el-dialog title="提示" :visible.sync="dialogVisible2" width="30%">
        <el-form
          :model="ruleForm2"
          status-icon
          :rules="rules"
          ref="ruleForm2"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="商品名称" prop="name">
            <el-input v-model="ruleForm2.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="price">
            <el-input v-model="ruleForm2.price" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="品牌" prop="band">
            <el-input v-model="ruleForm2.band" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-input v-model="ruleForm2.type" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="库存数量" prop="stock">
            <el-input v-model="ruleForm2.stock" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="备注" prop="remarks">
            <el-input v-model="ruleForm2.remarks" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm2('ruleForm2')"
              >提交</el-button
            >
            <el-button @click="cancelForm2">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { log } from "util";
export default {
  components: {},
  props: {},
  data() {
    return {
      checklist: { name: "", type: "", band: "" },
      bandvalue: [],
      typevalue: [],
      dialogVisible: false,
      dialogVisible2: false,
      ruleForm: {
        id: "",
        name: "",
        price: "",
        stock: "",
        band: "",
        type: "",
        remarks: ""
      },
      ruleForm2: {
        id: "",
        name: "",
        price: "",
        stock: "",
        band: "",
        type: "",
        remarks: ""
      },
      checkoptions: [
        {
          value: "品牌",
          label: "品牌",
          children: [{ value: "", label: "所有品牌" }]
        },
        {
          value: "商品类型",
          label: "商品类型",
          children: [{ value: "", label: "所有类型" }]
        }
      ],
      currentPage3: 1,
      shopname: "",
      page_size: 8,
      total: 0,
      options: [""],
      totalData: [],
      tableData: [],
      rules: {
        name: [
          { required: true, message: "请输入商品名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        price: [{ required: true, message: "请输入价格", trigger: "blur" }],
        stock: [{ required: true, message: "请输入库存数量", trigger: "blur" }]
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    check() {
      this.tableData = [];
      this.checklist.name = this.shopname;
      //console.log(this.checklist);
      var name = this.checklist.name;
      var band = this.checklist.band[0];
      var type = this.checklist.type[0];
      if (name) {
        if (band && type) {
          this.totalData.forEach(item => {
            if (item.band == band && item.type == type && name == item.name) {
              this.tableData.push(item);
            }
          });
        }
        if (!band && type) {
          this.totalData.forEach(item => {
            if (item.type == type && name == item.name) {
              this.tableData.push(item);
            }
          });
        }
        if (band && !type) {
          this.totalData.forEach(item => {
            if (item.band == band && name == item.name) {
              this.tableData.push(item);
            }
          });
        }
        if (!band && !type) {
          this.totalData.forEach(item => {
            if (name == item.name) {
              this.tableData.push(item);
            }
          });
        }
      } else {
        //如果不写商品名称
        if (band && type) {
          this.totalData.forEach(item => {
            if (item.band == band && item.type == type) {
              this.tableData.push(item);
            }
          });
        }
        if (!band && type) {
          this.totalData.forEach(item => {
            if (item.type == type) {
              this.tableData.push(item);
            }
          });
        }
        if (band && !type) {
          this.totalData.forEach(item => {
            if (item.band == band) {
              this.tableData.push(item);
            }
          });
        }
        if (!band && !type) {
          return;
        }
      }
    },
    add() {
      this.dialogVisible = true;
      console.log("添加新成员");
    },
    handleCurrentChange(val) {
      console.log(val);
      this.tableData = this.totalData.slice(
        this.page_size * (val - 1),
        this.page_size * val
      );
    },
    edit(data) {
      this.ruleForm2 = data;
      this.dialogVisible2 = true;
    },
    deleteUser(data) {
      console.log(data);
      var id = data.id;
      console.log(id);
      this.$confirm("您确定要删除该用户信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.totalData.forEach((item, index) => {
            if (item.id === id) {
              this.totalData.splice(index, 1);
            }
          });
          this.tabelData();
          return this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          return this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.ruleForm);
          this.totalData.unshift(this.ruleForm);
          this.dialogVisible = false;
          this.tabelData();
        } else {
          return this.$message({
            message: "请您填写准确的信息",
            type: "warning"
          });
        }
      });
    },
    cancelForm() {
      (this.ruleForm = {
        name: "",
        price: "",
        stock: "",
        band: "",
        type: "",
        remarks: ""
      }),
        (this.dialogVisible = false);
      this.$message("您已取消添加新成员");
    },
    getOptions() {
      var band = [];
      var type = [];
      this.totalData.forEach(item => {
        if (item.band) {
          band.push(item.band);
        }
        if (item.type) {
          type.push(item.type);
        }
      });
      band = [...new Set(band)];
      type = [...new Set(type)];
      band.forEach(item => {
        this.checkoptions[0].children.push({ value: item, label: item });
      });
      type.forEach(item => {
        this.checkoptions[1].children.push({ value: item, label: item });
      });
    },
    submitForm2(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          //console.log(this.ruleForm2);
          var id = this.ruleForm2.id;
          console.log(id);
          this.totalData.forEach(item => {
            if (item.id === id) {
              item = this.ruleForm2;
            }
          });

          this.dialogVisible2 = false;
          return this.$message({
            message: "修改成功",
            type: "success"
          });
        } else {
          return this.$message({
            message: "请您填写准确的信息",
            type: "warning"
          });
        }
      });
    },
    cancelForm2() {
      this.ruleForm = {};
      this.dialogVisible2 = false;
      this.$message("您已取消编辑");
    },
    handleChange(val) {
      this.checklist.band = val;
      console.log(val[0]);
      this.check();
    },
    handleChange2(val) {
      this.checklist.type = val;
      this.check();
    },
    backtabel() {
      this.tableData = this.totalData.slice(0, this.page_size);
      this.bandvalue = "";
      this.typevalue = "";
      this.shopname = "";
    },
    tabelData() {
      this.tableData = this.totalData.slice(0, this.page_size);
      this.total = this.totalData.length;
    },
    async getTotalGoods() {
      await this.$http.get("../../static/json/goods.json").then(res => {
        console.log(res.data);
        this.totalData = res.data;
      });
      this.tabelData();
      this.getOptions();
    }
  },
  created() {
    this.getTotalGoods();
  },
  mounted() {}
};
</script>
<style lang="scss" scoped>
.search {
  font-size: 16px;
  span {
    display: inline-block;
    margin-right: 17px;
  }
  .el-input {
    width: 200px;
  }
}
.clearfix {
  display: flex;
  justify-content: space-between;
}
</style>
